package com.ibm.fn.qa.toccata.suite1;

import java.util.HashSet;
import java.util.StringTokenizer;

import junit.framework.TestCase;

import com.ibm.fn.qa.toccata.conf.TestData;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.GetMethodWebRequest;

/**
 * visit RESTful service and retrieve workflow list on PE server.
 *
 * 
 */
public class TestWorkFlowList extends TestCase
{

	private HashSet originalWorkflowList = new HashSet();

	protected void setUp()
	{
	    System.out.println("setUp(), transfer some workflows to PE server ...");
	    
	    //transfer workflow to PE server and record the workflow name for validation in next steps.
	    // to be implemented ..., call PE API to transfer workflows.
	    originalWorkflowList.add("HR Workflow"); 
	    originalWorkflowList.add("Case Management Workflow");
	}
	
	protected void tearDown()
	{
	    System.out.println("tearDown(), clean PE server ...");
	    
	    // to be implemented ..., call PE API to delete the transferred workflows.
	    originalWorkflowList.clear();
	}
	
	public void testList() throws Exception
	{
        WebConversation wc = new WebConversation();
        WebRequest req = new GetMethodWebRequest(TestData.workflowResourceURL);
        WebResponse resp = wc.getResponse(req);
        
        assertEquals("Return content should be JSON format.", resp.getContentType().toLowerCase(), "application/json");
        assertEquals("Return code should be 200.", resp.getResponseCode(), 200);
        assertTrue("Workflow list should match the expected list.", validateResult(resp.getText()));

	}
	
	
	private boolean validateResult(String responseText) throws IllegalArgumentException
	{
	    HashSet currentWorkflowList = new HashSet();
	    
	    //result format is ["workflow1", "workflow2", ...]
	    responseText = responseText.trim();
	    if((!responseText.startsWith("[")) || (!responseText.endsWith("]")) || (responseText.length()<5))
	        throw new IllegalArgumentException("The format of response text is invalid, " + responseText);
	    responseText = responseText.substring(1, responseText.length()-1);
	    StringTokenizer st = new StringTokenizer(responseText, ",");
	    while(st.hasMoreTokens())
	    {
	        String oneToken = st.nextToken().trim();
	        if(oneToken.startsWith("\""))
	            oneToken = oneToken.substring(1, oneToken.length());
	        if(oneToken.endsWith("\""))
	            oneToken = oneToken.substring(0, oneToken.length()-1);
	        if(oneToken.length()>0)
	            currentWorkflowList.add(oneToken);
	    }
	    
	    //compare original workflows and current workflows in PE
	    if(currentWorkflowList.size() != originalWorkflowList.size())
	        return false;
	    if(!currentWorkflowList.containsAll(originalWorkflowList))
	        return false;
	    //
	    return true;
	}
}
